/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.ac.cput.pretorius.programs.program03.impl;

import za.ac.cput.pretorius.programs.program03.DateSelector;

/**
 *
 * @author griddy
 */
public class DateSelectorImpl implements DateSelector{
    
    
    @Override
    public boolean isValidDate(String date) {
        
        int day;
        int month;
        int year;
        
        boolean flag = true;
        if(date.length()!=8)
            flag = false;
        else
        {
        String dd = date.substring(0, 2);
        String mm = date.substring(2, 4);
        String yy = date.substring(4, 8);
        
        try
        {
            day = Integer.parseInt(dd);
            month = Integer.parseInt(mm);
            year = Integer.parseInt(yy);
            
            if(day <1 && day >31)
                throw new Exception();
            if(month<1 && month > 12)
                throw new Exception();
            
            switch(month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if(day > 31)
                        throw new Exception();
                    break;
                    
                case 4:
                case 6:
                case 9:
                case 11:
                    if(day > 30)
                        throw new Exception();
                    break;
                case 2:
                    if(day > 28)
                        if(day == 29)
                        {
                            if(year % 4 != 0)
                                throw new Exception();
                        }
                        else
                            throw new Exception();
                    break;
                   
            }
            
        }
        catch(Exception e)
        {
            flag = false;
        }
        
        
        
        
        }
        
        
        return flag;
    }
    
}
